<?php
class KhtuansController extends AppController {

	var $name = 'Khtuans';
	var $helpers = array('Html', 'Form');
    var $uses=array('Khtuan','Kehoach','Thuocdv','Zdv');
    var $components = array('RequestHandler');
    var $paginate=array('order'=>array('Khtuan.donvi_id','ngay'=>'asc'),'limit'=>100);
    
    /** 
     * Dành cho phòng ban tạo kế hoạch tuần
    * */
	function index($kehoach) {
		            
        $this->Khtuan->recursive = 0;
		$this->set('khtuans', $this->paginate(array('kehoach_id'=>$kehoach)));
        $this->Kehoach->recursive = 0;
		
        $kh=$this->Kehoach->find('first',array(
                    'conditions'=>array('Kehoach.id'=>$kehoach),
                    'fields'=>array('ngaybd','ngaykt','donvi_id','Donvi.tendv'),
                    'order'=>array('ngaybd'=>'asc')));
        
        $this->set(compact('kehoach','kh'));
	}
    
    function indexexcel($kehoach) {
       
        $this->Khtuan->recursive = 0;
		$this->set('khtuans', $this->Khtuan->find('all',
                array(
                    'conditions'=>array('kehoach_id'=>$kehoach),
                    'order'=>array('ngay'=>'asc'))));
        $this->Kehoach->recursive = 0;
		
        $kh=$this->Kehoach->find('first',array(
                    'conditions'=>array('Kehoach.id'=>$kehoach),
                    'fields'=>array('ngaybd','ngaykt','donvi_id','Donvi.tendv')));
        
        $tendv=$this->Zdv->find('first',array(
                'conditions'=>array('donvi_id'=>$kh['Kehoach'] ['donvi_id']),
                'fields'=>array('tendv','tendvcha'),'recursive'=>-1));
      
        $this->set(compact('kehoach','kh','tendv'));
        $this->layout='';
	}
    function search() {
        $this->Khtuan->recursive = -1;
        $khtuans=$this->paginate(
                    array('donvi_id'=>'/'));
        if(!empty($this->data)){
            $ngaybd = date('Y-m-d',strtotime($this->data['Khtuan']['ngaybd']));
            $ngaykt = date('Y-m-d',strtotime($this->data['Khtuan']['ngaykt']));
            $this->Khtuan->recursive = 0;
            $khtuans=$this->paginate(
                    array('Khtuan.donvi_id'=>$this->data['Khtuan']['donvi_id'],'ngay between ? and ?'=>array($ngaybd,$ngaykt)));
            
        }
		            
        $this->Donvi->recursive = -1;
        $donvis=$this->Donvi->find('list',array('fields'=>array('id','tendv')));
        
        $this->set(compact('kehoach','kh','donvis','khtuans'));
	}
    function searchcn() {
        $this->Khtuan->recursive = -1;
        $khtuans=$this->paginate(
                    array('donvi_id'=>'/'));
           if(!empty($this->data)){
            $ngaybd = date('Y-m-d',strtotime($this->data['Khtuan']['ngaybd']));
            $ngaykt = date('Y-m-d',strtotime($this->data['Khtuan']['ngaykt']));
            $this->Thuocdv->recursive = -1;
            $dl=array();
           
            $dl=$this->Thuocdv->find('list',array(
                        'conditions'=>array('donvi_id'=>
                                    $this->data['Khtuan']['donvi_id']),
                        'fields'=>array('donvi','donvi')));
            $this->Khtuan->recursive = 0;
          //  $paginate=array('order'=>array('Donvi.tendv'=>'asc'));
            
            $khtuans=$this->paginate(
                    array('Khtuan.donvi_id'=>$dl,'ngay between ? and ?'=>array($ngaybd,$ngaykt)));
            
        }          
    
		$cn=array();            
        $this->Thuocdv->recursive = 0;
    
        $cn=$this->Thuocdv->find('list',array(
                'fields'=>array('donvi_id','donvi_id')
                ));
                
    //    print_r($cn);
        $this->Donvi->recursive = -1;        
        
        $donvis=$this->Donvi->find('list',array(
                'fields'=>array('Donvi.id','Donvi.tendv'),
                'conditions'=>array('Donvi.id'=>$cn)));
        
        $this->set(compact('kehoach','kh','donvis','khtuans'));
	}
    function hienkq(){
        $ngaybd='';
        $ngaykt='';
        $dv='';
        
        $this->layout='ajax';
      //  debug($this->data);
        $this->Khtuan->recursive = -1;
        $khtuans=$this->paginate(
                    array('donvi_id'=>'/'));
        if(!empty($this->data)){
            $ngaybd = date('Y-m-d',strtotime($this->data['Khtuan']['ngaybd']));
            $ngaykt = date('Y-m-d',strtotime($this->data['Khtuan']['ngaykt']));
            $dv=$this->data['Khtuan']['donvi_id'];
            $this->Thuocdv->recursive = -1;
            
           
            $dl=$this->Thuocdv->find('list',array(
                        'conditions'=>array('donvi_id'=>$dv),
                        'fields'=>array('donvi','donvi')));
            $this->Khtuan->recursive = 0;
          //  $paginate=array('order'=>array('Donvi.tendv'=>'asc'));
            
            $this->paginate=array(
                'conditions'=>array('Khtuan.donvi_id'=>$dl,'ngay between ? and ?'=>array($ngaybd,$ngaykt)),
               // 'order'=>array('ngay'=>'asc')
                );
            $khtuans=$this->paginate('Khtuan');
            
        }
        //print_r('du lieu'.$this);
        $this->set(compact('kehoach','kh','donvis','khtuans','ngaybd','ngaykt','dv'));
    }
    
    function hienkqexcel($ngaybd,$ngaykt,$dv){
             
        $this->layout='';
      //  debug($this->data);
        
    
        
        $donvi=$this->Donvi->find('first',array(
                'fields'=>array('tendv'),
                'conditions'=>array('id'=>$dv),
                'recursive'=>-1));    
        
        $this->Thuocdv->recursive = -1;   
        $dl=$this->Thuocdv->find('list',array(
                        'conditions'=>array('donvi_id'=>$dv),
                        'fields'=>array('donvi','donvi')));
        $this->Khtuan->recursive = 0;
          //  $paginate=array('order'=>array('Donvi.tendv'=>'asc'));
            
        $khtuans=$this->Khtuan->find('all',array(
                'conditions'=>array('Khtuan.donvi_id'=>$dl,
                        'ngay between ? and ?'=>array($ngaybd,$ngaykt))));
     //   $tendv=$this->Zdv->find('first',array(
       //         'conditions'=>array('donvi_id'=>$dv),
         //       'fields'=>array('tendv','tendvcha'),'recursive'=>-1));   
        
        //print_r('du lieu'.$this);
        $this->set(compact('kh','donvis','khtuans','ngaybd','ngaykt','dv','donvi','tendv'));
    }
    /*
    function searchcn() {
        $this->Khtuan->recursive = -1;
        $khtuans=$this->paginate(
                    array('donvi_id'=>'/'));
                    
        if(!empty($this->data)){
            $ngaybd = date('Y-m-d',strtotime($this->data['Khtuan']['ngaybd']));
            $ngaykt = date('Y-m-d',strtotime($this->data['Khtuan']['ngaykt']));
            $this->Thuocdv->recursive = -1;
            $dl=array();
           
            $dl=$this->Thuocdv->find('list',array(
                        'conditions'=>array('donvi_id'=>
                                    $this->data['Khtuan']['donvi_id']),
                        'fields'=>array('donvi','donvi')));
            $this->Khtuan->recursive = 0;
          //  $paginate=array('order'=>array('Donvi.tendv'=>'asc'));
            
            $khtuans=$this->paginate(
                    array('Khtuan.donvi_id'=>$dl,'ngay between ? and ?'=>array($ngaybd,$ngaykt)));
            
        }
		$cn=array();            
        $this->Thuocdv->recursive = 0;
    
        $cn=$this->Thuocdv->find('list',array(
                'fields'=>array('donvi_id','donvi_id')
                ));
                
    //    print_r($cn);
        $this->Donvi->recursive = -1;        
        
        $donvis=$this->Donvi->find('list',array(
                'fields'=>array('Donvi.id','Donvi.tendv'),
                'conditions'=>array('Donvi.id'=>$cn)));
        
        $this->set(compact('kehoach','kh','donvis','khtuans'));
	}
    */
	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid Khtuan.', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->set('khtuan', $this->Khtuan->read(null, $id));
	}

	function add($kehoach,$id_dv) {
		$this->Khtuan->recursive = 0;
		$this->set('khtuans', $this->paginate(array('kehoach_id'=>$kehoach)));
        $this->set(compact('kehoach'));
        
        $kh=$this->Kehoach->find('first',array(
                    'conditions'=>array('Kehoach.id'=>$kehoach),
                    'fields'=>array('ngaybd','ngaykt','donvi_id','Donvi.tendv')));
        
        if (!empty($this->data)) {
			$this->Khtuan->create();
            $this->data['Khtuan']['kehoach_id']=$kehoach;
            $this->data['Khtuan']['donvi_id']=$id_dv;
            
            if(strtotime($this->data['Khtuan']['ngay'])<strtotime($kh['Kehoach']['ngaybd'])
               || strtotime($this->data['Khtuan']['ngay'])>strtotime($kh['Kehoach']['ngaykt']))
             
				$this->Session->setFlash(__('Không thể nhập ngày lọt ngoài khoảng thời gian của tuần', true));
			else{	
                if ($this->Khtuan->save($this->data)) {
				    $this->Session->setFlash(__('Công việc được tạo!', true));
				    $this->redirect(array('action'=>'index',$kehoach));
                } else {
				    $this->Session->setFlash(__('Có lỗi trong khi lưu, vui lòng thử lại', true));
                }
            }
		}
        
                    
		$dvts = $this->Khtuan->Dvt->find('list',array('fields'=>array('id','donvitinh')));
		$donvis = $this->Khtuan->Donvi->find('list');
		$kehoaches = $this->Khtuan->Kehoach->find('list');
		$this->set(compact('dvts', 'donvis', 'kehoaches','kehoach','id_dv','kh'));
	}

	function edit($id = null,$kehoach) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid Khtuan', true));
			$this->redirect(array('action'=>'index'));
		}
    
        $this->Khtuan->recursive = 0;
		$this->set('khtuans', $this->paginate(array('kehoach_id'=>$kehoach)));
        $this->set(compact('kehoach'));
        
        $kh=$this->Kehoach->find('first',array(
                    'conditions'=>array('Kehoach.id'=>$kehoach),
                    'fields'=>array('ngaybd','ngaykt','donvi_id','Donvi.tendv')));
	
    	if (!empty($this->data)) {
		
            $this->data['Khtuan']['kehoach_id']=$kehoach;
          //  $this->data['Khtuan']['donvi_id']=$id_dv;
            if(strtotime($this->data['Khtuan']['ngay'])<strtotime($kh['Kehoach']['ngaybd'])
               || strtotime($this->data['Khtuan']['ngay'])>strtotime($kh['Kehoach']['ngaykt']))
             
				$this->Session->setFlash(__('Không thể nhập ngày lọt ngoài khoảng thời gian của tuần', true));
			else{
			     if ($this->Khtuan->save($this->data)) {
				    $this->Session->setFlash(__('Công việc được tạo!', true));
				    $this->redirect(array('action'=>'index',$kehoach));
			     }    else {
				    $this->Session->setFlash(__('The Khtuan could not be saved. Please, try again.', true));
			     }
            }
		}
	
    	if (empty($this->data)) {
			$this->data = $this->Khtuan->read(null, $id);
		}
	   
                    
		$dvts = $this->Khtuan->Dvt->find('list',array('fields'=>array('id','donvitinh')));
		$donvis = $this->Khtuan->Donvi->find('list');
		$kehoaches = $this->Khtuan->Kehoach->find('list');
		$this->set(compact('dvts', 'donvis', 'kehoaches','kehoach','id_dv','kh','id'));
	}

	function delete($id = null,$kehoach) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for Khtuan', true));
			$this->redirect(array('action'=>'index',$kehoach));
		}
		if ($this->Khtuan->delete($id)) {
			$this->Session->setFlash(__('Công việc được xóa!', true));
			$this->redirect(array('action'=>'index',$kehoach));
		}
	}

}
?>